package com.example.springbootdemo.Controller;

import com.auth0.jwt.JWT;
import com.example.springbootdemo.entity.VO.LoginUser;
import com.example.springbootdemo.utils.ApiResult;
import com.example.springbootdemo.utils.JWTUtils;
import com.example.springbootdemo.utils.PassToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@CrossOrigin
@RestController
public class testController {
    @Autowired
    RedisTemplate<String,Object> redisTemplate;  //提供的泛型类
    //测试类
    @GetMapping("/test")
    public String test(){
        //读取redis中的数据
        String s =(String) redisTemplate.opsForValue().get("dataname");   //key的值是name  括号是强制转换  将数据强制转换成字符串类型  set方法设置数据  get方法获取数据
        redisTemplate.opsForValue().set("age","21",20, TimeUnit.SECONDS);    //键名  键值   过了 2000ms后消失
        return s;
    }

    @PostMapping("/login1")
    public ApiResult<String> login(@RequestBody LoginUser loginUser){
        //先判断验证码是否正确
        //1如何从redis取出保存的验证码
        String storeCode = (String) redisTemplate.opsForValue().get("captcha_codes:"+loginUser.getUuid());
        //2用户输入的验证码和storeCode比对
        if(loginUser.getCode().equals(storeCode)){
            String id = loginUser.getUsername();
            String pwd = loginUser.getPassword();
            Map<String,String> map = new HashMap<>();
            map.put("username",id);
            map.put("password",pwd);
            if(id.equals("admin")&&pwd.equals("admin1111")){  //比对账号和密码
                String token = JWTUtils.getToken(map);  //getToken方法中 map<String,String>
                return ApiResult.createBySuccess(200,token,"登录成功");
            }
            return ApiResult.createBySuccess(500,"","账号或密码错误");
        }else{
            return ApiResult.createBySuccess(500,"","验证码错误");
        }

    }
    @PassToken
    @GetMapping("/getStudent1")
    public ApiResult<String> getStudent(){

        return ApiResult.createBySuccess(200,"","登录成功");
    }

    @GetMapping("/getInfo1")
    public ApiResult<String> getInfo(HttpServletRequest request){
//      假造用户信息   加载用户信息
//        String id = JWT.decode(token).getClaim("username").asString();    //获取用户的信息
//        System.out.println(token+"     ");
        String token = request.getHeader("Authorization");
        System.out.println(token+"    ");
        String id = JWT.decode(token.substring(token.lastIndexOf(" ")+1)).getClaim("username").asString();
//        Map<String,Object> map = new HashMap<>();
//        map.put("name","张三");
//        map.put("avatar","1.jpg");
        return ApiResult.createBySuccess(200,id,"登录成功");
    }
    @PassToken
    @GetMapping("/getBanners")
    public ApiResult<List<String>> getBanners(){
        List<String> list = new ArrayList<>();
        list.add("images/1.jpg");
        list.add("images/2.jpg");
        list.add("images/3.jpg");
        return ApiResult.createBySuccess(200,list,"显示轮播图");
    }
}
